//
//  GCMURLViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/4/28.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMURLViewController.h"
#import "QRadioButton.h"
#import "GCMMainViewController.h"
#import "GCMMain1ViewController.h"



@interface GCMURLViewController ()<UITextFieldDelegate>

@end

@implementation GCMURLViewController{

    UITextField *_urlText;
    UILabel *_urlLabel;
    QRadioButton *_zl1;
    QRadioButton *_zl2;
}
-(void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBarHidden = NO;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];
}

-(void)createUI{
    
    self.view.backgroundColor  =[ UIColor whiteColor];

    UILabel *label  = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH - 20,30)];
    label.text = @"服务器URL:";
    label.textColor = [UIColor lightGrayColor];
    [self.view addSubview:label];
    
    _urlText =[[UITextField alloc]initWithFrame:CGRectMake(10, 130, SCREEN_WIDTH- 20, 40)];
    NSUserDefaults *s = [NSUserDefaults standardUserDefaults];
    NSString *str =  [s valueForKey:@"URL"];
    if ([str isEqualToString:@""]) {
        _urlText.placeholder= @"http://0.89892528.cn:8700/";
    }else{
        _urlText.text = str;
    }
    
    _urlText.layer.cornerRadius =10;
    _urlText.textColor = [UIColor lightGrayColor];
    _urlText.layer.borderColor =[UIColor blackColor].CGColor;
    _urlText.backgroundColor =[UIColor whiteColor];
    _urlText.clearButtonMode = UITextFieldViewModeAlways;
    _urlText.tag = 1;
    _urlText.delegate=self;
//    添加提示的label
    _urlLabel  = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH- 40, 40)];
    _urlLabel.text = @"http://0.89892528.cn:8700/";

    [self.view addSubview:_urlText];
    
    UILabel *labelLine  = [[UILabel alloc]initWithFrame:CGRectMake(10, 171, SCREEN_WIDTH - 20,2)];
    labelLine.textColor = [UIColor orangeColor];
    labelLine.layer.borderWidth = 1;
    labelLine.layer.borderColor = [[UIColor orangeColor]CGColor];
    [self.view addSubview:labelLine];

    
    //  确定按钮
    UIButton * logbutton =[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 180, 100, 40)];
    [logbutton setTitle:@"确定" forState:UIControlStateNormal];
    logbutton.tag = 9999;
    logbutton.backgroundColor = [UIColor orangeColor];
    logbutton.layer.cornerRadius = 5;
    [logbutton addTarget:self action:@selector(logOKClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logbutton];
    
    
    UILabel *moshiLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 250, SCREEN_WIDTH - 20,30)];
    moshiLabel.text = @"请选择模式:";
    moshiLabel.textColor = [UIColor lightGrayColor];
    [self.view addSubview:moshiLabel];
    
    //质量
    _zl1 = [[QRadioButton alloc] initWithDelegate:self groupId:@"groupId1"];
    _zl1.frame = CGRectMake(10, 280, self.view.bounds.size.width/3, 50);
    [_zl1 setTitle:@"Win10风格" forState:UIControlStateNormal];
    [_zl1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_zl1.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
    [self.view addSubview:_zl1];
    
    _zl2 = [[QRadioButton alloc] initWithDelegate:self groupId:@"groupId1"];
    _zl2.frame = CGRectMake(self.view.bounds.size.width/3 + 10, 280 , self.view.bounds.size.width/3, 50);
    [_zl2 setTitle:@"现代风格" forState:UIControlStateNormal];
    [_zl2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_zl2.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]];
    [self.view addSubview:_zl2];
    
    [_zl2 setChecked:YES];
    NSUserDefaults *sty = [NSUserDefaults standardUserDefaults];
    NSString *stylestr = [s valueForKey:@"style"];
    if ([stylestr isEqualToString:@"Window风格"]) {
        [_zl1 setChecked:YES];
    }else{
        [_zl2 setChecked:YES];
    }
    
    
    UIButton * button  = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 330, 100, 40)];
    [button addTarget:self action:@selector(onMoshiButtonclick) forControlEvents:UIControlEventTouchUpInside];
    button.backgroundColor = [UIColor orangeColor];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    button.titleLabel.textColor = [UIColor whiteColor];
    button.layer.cornerRadius = 5;
    [self.view addSubview:button];
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{

    [_urlLabel removeFromSuperview];
}

-(void)logOKClick{
    
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"arr"];
    if ([_urlText.text isEqualToString:@""]) {
        NSUserDefaults *s = [NSUserDefaults standardUserDefaults];
        [s setObject:_urlLabel.text forKey:@"URL"];
        [s synchronize];
        [self.navigationController popViewControllerAnimated:YES];
    }else{
    
        NSUserDefaults *s = [NSUserDefaults standardUserDefaults];
        [s setObject:_urlText.text forKey:@"URL"];
        [s synchronize];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    //[[NSUserDefaults standardUserDefaults]setValue:_urlText.text forKey:@"URL"];
    
}
-(void)onMoshiButtonclick{

    if (_zl1.checked == NO) {
        GCMMain1ViewController *main =[[GCMMain1ViewController alloc]init];
        
        NSUserDefaults *s = [NSUserDefaults standardUserDefaults];
        [s setObject:@"现代风格" forKey:@"style"];
        [s synchronize];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:main];
        window.rootViewController = navc;
        
    }else{
        GCMMainViewController *main =[[GCMMainViewController alloc]init];
        
        NSUserDefaults *s = [NSUserDefaults standardUserDefaults];
        [s setObject:@"Window风格" forKey:@"style"];
        [s synchronize];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:main];
        window.rootViewController = navc;
    }
}

//点击return收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
